package com.chanzany.composite;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

//抽象构件：物品
public interface Articles {
    public float calculation();
    public void show();
}
//树叶构件：商品
@AllArgsConstructor
@Setter
@Getter
class Goods implements Articles{
    private String name;
    private int quantity;
    private float unitPrice;

    @Override
    public float calculation() {
        return quantity * unitPrice;
    }

    @Override
    public void show() {
        System.out.println(name + "(数量：" + quantity + "，单价：" + unitPrice + "元)");
    }
}
//树枝构件：袋子
@AllArgsConstructor
class Bags implements Articles{
    private final String name;
    private final ArrayList<Articles> bags = new ArrayList<>();
    public void add(Articles c){
        bags.add(c);
    }
    public void remove(Articles c){
        bags.remove(c);
    }
    public Articles getChild(int i){
        return bags.get(i);
    }
    public float calculation(){
        float s = 0;
        for (Articles bag : bags) {
            s += bag.calculation();
        }
        return s;
    }

    @Override
    public void show() {
        for (Articles bag : bags) {
            bag.show();
        }
    }
}